import uploadAliyun from "../../../UploadAliyun/uploadAliyun";
const app = getApp();
Page({
  data: {
    xhx_images: app.xhx_images,
    xhx_route: app.xhx_route,
    iphone: app.ifiPhone(),
    form: {
      "id": "",
      "userId": "",
      "username": "",
      "headPortrait": "",
      "telephone": "",
      "sex": 0,
      "height": "",
      "weight": "",
      "birthday": "",
      "address": "",
      "longitude": "",
      "latitude": "",
      "state": "",
      "signLabel": "",
      "createTime": "",
      "updateTime": ""
    }
  },
  onLoad,
  userDetail, //查看用户信息
  changeAvat, //修改头像
  changeSex, //修改性别
  changeDate, //生日选择
  comfirm,
  push2Form,
})

function onLoad() {
  const that = this;
  that.userDetail();
}

function userDetail() {
  const that = this;
  app.middleware.requestAjax({
    serAddr: app.serAddrs.userDetail,
    success: function(res) {
      var temp = res.data;
      that.setData({
        oldData: res.data
      })
      that.push2Form(temp)
    },
    fail: function(res) {
      app.middleware.showToast(res && res.msg || "加载失败");
    }
  })
}

function changeAvat() {
  const that = this;
  wx.chooseImage({
    count: 1,
    success: function(res) {
      const tempFilePaths = res.tempFilePaths;


      uploadAliyun(
        tempFilePaths[0],
        function(res) {
          console.log("上传成功", res)
          const headPortrait = res;
          that.push2Form({
            headPortrait
          })
        },
        function(res) {
          console.log("上传失败")
          wx.showToast({
            title: '上传失败',
            icon: "none"
          })
        }
      )
    },
  })
}

function changeSex() {
  const that = this;
  wx.showActionSheet({
    itemList: ["男", "女"],
    success: function(res) {
      const tapIndex = res.tapIndex;
      let sex = 1;
      if (tapIndex == 1) sex = 2;
      that.push2Form({
        sex
      })
    }
  })
}

function changeDate(event) {
  const that = this;
  const birthday = event.detail.value;
  that.push2Form({
    birthday
  })
}

function comfirm(event) {
  const that = this;
  const oldData = that.data.oldData || {};
  const submitForm = event.detail.value;
  let form = that.data.form || {};
  Object.assign(form, submitForm);

  oldData.headPortrait = oldData.headPortrait || "";
  oldData.username = oldData.username || "";
  oldData.sex = oldData.sex || "";
  oldData.birthday = oldData.birthday || "";
  oldData.address = oldData.address || "";
  oldData.signLabel = oldData.signLabel || "";

  console.log("oldData.headPortrait", oldData.headPortrait)
  console.log("form.headPortrait", form.headPortrait)

  console.log("oldData.username", oldData.username)
  console.log("form.username", form.username)

  console.log("oldData.sex", oldData.sex)
  console.log("form.sex", form.sex)

  console.log("oldData.birthday", oldData.birthday)
  console.log("form.birthday", form.birthday)

  console.log("oldData.address", oldData.address)
  console.log("form.address", form.address)

  console.log("oldData.signLabel", oldData.signLabel)
  console.log("form.signLabel", form.signLabel)


  if (oldData.headPortrait == form.headPortrait &&
    oldData.username == form.username &&
    oldData.sex == form.sex &&
    oldData.birthday == form.birthday &&
    oldData.address == form.address &&
    oldData.signLabel == form.signLabel) {
    return;
  }

  if (form.height == '') {
    form.height = 175;
    if (form.sex == 2) form.height = 162;
  }
  if (form.weight == '') {
    form.weight = 65;
    if (form.sex == 2) form.weight = 55;
  }

  if (!form.username.trim()) return app.middleware.showToast("请输入姓名");
  if (Number(form.height) < 50 || Number(form.height) > 250) return app.middleware.showToast("请输入范围内身高(50-250)");
  if (Number(form.weight) < 30 || Number(form.weight) > 200) return app.middleware.showToast("请输入范围内体重(30-200)");


  app.middleware.requestAjax({
    serAddr: app.serAddrs.userUpdate,
    data: form,
    success: function(res) {
      that.setData({
        oldData: JSON.parse(JSON.stringify(form))
      })
    },
    complete: function(res) {
      app.middleware.showToast(res && res.msg || "加载失败");
    }
  })
}

function push2Form(data = {}) {
  const that = this;
  var form = that.data.form || {};
  Object.assign(form, data);
  that.setData({
    form: form
  })
}